<template>
	<tm-app ref="app">
		<tm-alert v-if="showWin" color="pink" linearDeep="accent" linear="right" :content="content2" :height="140"></tm-alert>
		
		
		<tm-carousel autoplay :margin="[0,16]" align="right" :round="1" :width="700" :height="700"
			:list="listimg"></tm-carousel>
		<tm-sheet>
					<tm-text :font-size="24" _class="text-weight-b" :label="name"></tm-text>
				</tm-sheet>
		
		<view class="mb-32 mx-32 round-3 overflow">
			<tm-cell :margin="[0, 0]" :titleFontSize="30" title="price:">
				<template v-slot:right><tm-text :label="school"></tm-text></template>
			</tm-cell>
		</view>
		

		
		<tm-tabbar :autoSelect="true" v-model:active="acc">
			<tm-button @click="changPos()" color="red" :width="720" label="buy now"></tm-button>
		</tm-tabbar>


		<tm-message ref="msg" :lines="5"></tm-message>
	</tm-app>
</template>


<script lang="ts" setup>
	import { ref, nextTick } from "vue"
	import { onLoad } from "@dcloudio/uni-app"
	import { isEmpty } from "@/tmui/tool/function/util";
	import { bsDetail, bsOrderTime, getBsSubject, manSubject, manTime, getBsDate, isShowBsOrderTimeDetail } from '@/api/detail'
	import { manOnlyBsSel } from '@/api/search'
	import { saveEntity } from '@/api/order'
	import tmMessage from "@/tmui/components/tm-message/tm-message.vue";

	const msg = ref<InstanceType<typeof tmMessage> | null>(null)
	const acc = ref(1);
	const id = ref("");
	const name = ref("");
	const remark = ref("");
	const listimg = ref([]);
	const school = ref("");
	const subject = ref("");
	const ordercount = ref("");
	const discount = ref("");
	const showWin = ref(false);
	const pos = ref("bottom");
	const timelist = ref([]);
	const datelist = ref([]);
	const subjectlist = ref([]);
	const remarks = ref("");
	const userid = ref("");
	const subjectResult = ref("");
	const timeResult = ref("");
	const placement = ref('top');
	const date = ref("");
	const xyFlag = ref(false);
	const bsDate = ref(false);
	const bsDateDetail = ref("");
	const content2 = ref([
		{
			title: 'Purchase reminder',
			content: 'Payments are being developed;instagram:'
		}
	])

	onLoad((e) => {
		id.value = e.id;
		userid.value = uni.getStorageSync("userid");
		loadDetail();// 加载帮手基础数据
		// loadBsTime();// 加载帮手可以安排的合理时间
		// loadBsDate();// 进行加载帮手的预约日期 datelist
		// queryBsSubject();
	})

	function xyFlagClick(e : any) {
		xyFlag.value = e;
	}

	function loadXyContent() {
		uni.navigateTo({
			url: `/pages/news/xynews?id=11`
		})
	}

	function loadEval() {
		uni.navigateTo({
			url: `/pages/detail/showEval?id=${id.value}`
		})
	}

	const loadBsDate = async () => {
		let param = { "id": id.value };
		const result = await getBsDate(param);
		// console.log(JSON.stringify(result));
		datelist.value = result;
	}

	const queryBsSubject = async () => {
		let param = { "id": id.value }
		const result = await getBsSubject(param);
		subjectlist.value = result;
	}


	const loadBsTime = async () => {
		let param = { "id": id.value, "date": date.value }
		const result = await bsOrderTime(param);
		timelist.value = result.list;
		date.value = result.date;
	}

	const loadDetail = async () => {
		let param = { "id": id.value }
		const result = await bsDetail(param);
		name.value = result.name;
		remark.value = result.remark;
		listimg.value = JSON.parse(result.xszImg);
		school.value = result.school;
		subject.value = result.subject;
		ordercount.value = result.ordercount;
		discount.value = result.discount;
	}

	const managerBsDateStatus = async (status : string, dates : string, ids : string) => {
		date.value = dates;
		let param = { "status": status, "id": ids, "list": datelist.value };
		const result = await manOnlyBsSel(param);
		// console.log(JSON.stringify(result));
		datelist.value = result.list;
		bsDate.value = false;
		loadBsTime();
		loadBsDateDetail();
	}

	const loadBsDateDetail = async () => {
		let param = { "bsid": id.value, "date": date.value };
		const result = await isShowBsOrderTimeDetail(param);
		console.log(result);
		if (result.status === "1") {
			bsDate.value = true;
			bsDateDetail.value = result.msg;
		}
	}

	function managerBsTimeStatus(status : string, time : string) {
		mtime(status, time);
	}

	const mtime = async (status : string, time : string) => {
		let param = { "status": status, "time": time, "list": timelist.value };
		const result = await manTime(param);
		// console.log(JSON.stringify(result));
		timelist.value = result.list;
		timeResult.value = result.timeResult;
	}


	function managerSubject(subjectid : string, subjectname : string, status : string) {
		subjectResult.value = subjectname;
		msubject(subjectid, subjectname, status);
	}



	const msubject = async (subjectid : string, subjectname : string, status : string) => {
		let param = { "subjectid": subjectid, "subjectname": subjectname, "status": status, "list": subjectlist.value };
		const result = await manSubject(param);
		subjectlist.value = result;
	}

	
	function changPos() {
		// console.log('vvvvvvvvvv',userid.value);
		if (isEmpty(userid.value)) {
			msg.value.show({ model: 'error',text: 'Please log in first!' })
			return false;
		}
		saveOrder();
		return true;
	}

	

	const saveOrder = async () => {
		let param = { "wxid": userid.value, "bsid": id.value};
		const result = await saveEntity(param); // ysxm
		let orderid = result.msg.id;
		uni.navigateTo({
			url: `/pages/order/orderDetailPay?id=${orderid}`
		})
	}


	function backPage(e : any) {
		// console.log(JSON.stringify(e));
		uni.redirectTo({
			url: '/pages/index/seach'
		})
	}
</script>


<style>
	.btnCss {
		background-color: orange;
		color: #FFF;
		height: 80rpx;
		margin-top: 20rpx;
		font-size: 30rpx;
		width: 100%;
	}
</style>